package test03;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class SendUDP {
	public static final byte[] DEST_ADDR = new byte[]{(byte) 158, (byte) 130, (byte) 67, (byte) 216};
	public static final String DEST_HOSTNAME = "ampersand";
	public static final int DEST_PORT = 4505;
	
	public static void main(String str[]) {
		getNameAndIP();
		sendUDP();
	}
	
	public static void getNameAndIP() {
		InetAddress address, destAddr;
		try {
			address = InetAddress.getLocalHost();
			System.out.println("===SendUDP===");
			System.out.println("Local Host: " + address.getHostName() + ", " + address.getHostAddress());
			destAddr = InetAddress.getByAddress(DEST_HOSTNAME, DEST_ADDR);
			System.out.println("Dest Addr: " + destAddr.getHostName() + ", " + destAddr.getHostAddress());
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}
	
	public static void sendUDP(){
		try {
			System.out.println(System.currentTimeMillis());
			InetAddress destAddr = InetAddress.getByAddress(DEST_ADDR);
			//byte[] testBytes = new byte[]{(byte) 1, (byte) 2, (byte) 3, (byte) 99};
			byte[] testBytes = new byte[512];
			DatagramPacket message = new DatagramPacket(testBytes, testBytes.length, destAddr, DEST_PORT);
			DatagramSocket sock = new DatagramSocket();
			sock.send(message);
			sock.close();
			System.out.println(System.currentTimeMillis());
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
